#include "checkerframe.h"

const int CheckerFrame::ID_MENU_PANEL=120;
const int CheckerFrame::ID_SELECT_GAME_PANEL=121;
const int CheckerFrame::ID_GAME_PANEL=123;

CheckerFrame::CheckerFrame(const wxString&title) : wxFrame(NULL,wxID_ANY,title,wxPoint(0,0),wxSize(1024,700))
{
    this->SetMinSize(wxSize(1024,700));
    this->SetMaxSize(wxSize(1024,700));
    this->EnableCloseButton(false);
    //SetIcon
    wxImage::AddHandler(new wxPNGHandler);
    SetIcon(wxIcon(wxT("source\\icongame.png"),wxBITMAP_TYPE_PNG));

    //PutSelectMenu
    menupanel = new MenuPanel(this,ID_MENU_PANEL);

//    Put Menu Panel
//    selectgamepanel = new SelectGamePanel(this,ID_SELECT_GAME_PANEL);
//    this->RemoveChild(selectgamepanel);




//    int **arr,i,j;
//    arr = (int**)malloc(8*sizeof(int));
//    for(i=0;i<8;++i)
//    {
//        arr[i]=(int*)malloc(8*sizeof(int));
//    }
//    for(i=0;i<8;++i)
//    {
//        for(j=0;j<8;++j)
//        {
//            arr[i][j]=Dam::ID_BLACK_DAM;
//        }
//    }
//
//    BoardPanel *boardpanel = new BoardPanel(this,-1,8,0,arr);



//    MenuPanel = new wxPanel(this,wxID_ANY);
//    wxImage::AddHandler(new wxPNGHandler);
//    wxBitmap exit(wxT("source\\exit2.png"),wxBITMAP_TYPE_PNG);
//    MenuPanel->SetBackgroundBitmap(exit);
//    this->RemoveChild(MenuPanel);
//    this->AddChild(MenuPanel);


//    GamePanel = new wxPanel(this,wxID_ANY);
//    wxBitmap flower(wxT("source\\flower.png"),wxBITMAP_TYPE_PNG);
//    GamePanel->SetBackgroundBitmap(flower);


    Connect(wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(CheckerFrame::OnHandleEvent));

    Centre();
}


void CheckerFrame::OnHandleEvent(wxCommandEvent&event)
{
    if (event.GetId()==menupanel->ID_QUIT_BUTTON)
    {
        wxMessageDialog *dial = new wxMessageDialog(NULL,wxT("Are you sure want to quit?"),wxT("Question"),wxYES_NO|wxNO_DEFAULT|wxICON_QUESTION,wxPoint(300,350));
        if (dial->ShowModal()==wxID_YES)
        {
            this->Close(true);
        }
    }
    else if (event.GetId()==menupanel->ID_NEW_BUTTON)
    {
        this->RemoveChild(menupanel);
        delete(menupanel);

        Refresh();
        this->Iconize(true);
        selectgamepanel = new SelectGamePanel(this,ID_SELECT_GAME_PANEL);
//        this->AddChild(selectgamepanel);

//        wxPuts(wxT("NEW\n"));
    }
    else if (event.GetId()==selectgamepanel->ID_BACK_TO_MENU_PANEL)
    {
        this->RemoveChild(selectgamepanel);
//        selectgamepanel->Reparent(NULL);

//        wxPuts(wxT("Mendelete\n"));
//        selectgamepanel->DeleteAll();
        delete(selectgamepanel);
//        wxPuts(wxT("Selesai Mendelete\n"));

        Refresh();
        this->Iconize(true);
        menupanel = new MenuPanel(this,ID_MENU_PANEL);
//        this->AddChild(menupanel);
    }
    else if (event.GetId()==selectgamepanel->ID_START_GAME)
    {
        wxPuts(wxT("Nyampe frame start game\n"));
//        this->RemoveChild(selectgamepanel);
//        delete(selectgamepanel);
//        Refresh();
//        this->Iconize(true);

        //construct gamepanel
//        GamePanel::GamePanel(wxFrame*frame,int id,bool isnewgame,wxString sourcefile,int boardsize,
//          int gametype,wxString playername,int greedytype,int greedytype1,int greedytype2,int gamespeed)

        bool isnewgame=true;
        wxString sourcefile=wxT("");
        int boardsize;
        if (selectgamepanel->selectgamedialog->rbboard1->GetValue())
        {
            boardsize=8;
        }
        else if (selectgamepanel->selectgamedialog->rbboard2->GetValue())
        {
            boardsize=10;
        }
        wxPrintf(wxT("boardsize : %d\n"),boardsize);
        int gametype;
        wxString playername;
        int greedytype;
        int greedytype1;
        int greedytype2;
        int gamespeed;

        wxPuts(wxT("d1\n"));
        if (selectgamepanel->gametype==SelectGamePanel::ID_PLAYER_VERSUS_AI)
        {
            wxPuts(wxT("d2\n"));
            gametype = GamePanel::playervsai;
            wxPuts(wxT("d7\n"));
            playername = selectgamepanel->selectgamedialog->nametext->GetValue();
            wxPuts(wxT("d8\n"));
            if (selectgamepanel->selectgamedialog->tbtngreedyfront->GetValue())
            {
                greedytype=GamePanel::greedybyfront;
            }
            else if (selectgamepanel->selectgamedialog->tbtngreedyback->GetValue())
            {
                greedytype=GamePanel::greedybyback;
            }
            wxPuts(wxT("d9\n"));
            //construct gamepanel pvsai;
        }
        else if (selectgamepanel->gametype==SelectGamePanel::ID_AI_VERSUS_AI)
        {
            wxPuts(wxT("d4\n"));
            gametype = GamePanel::aivsai;
            if (selectgamepanel->selectgamedialog->tbtngreedyfront1->GetValue())
            {
                greedytype1=GamePanel::greedybyfront;
            }
            else if (selectgamepanel->selectgamedialog->tbtngreedyback1->GetValue())
            {
                greedytype1=GamePanel::greedybyback;
            }

            if (selectgamepanel->selectgamedialog->tbtngreedyfront2->GetValue())
            {
                greedytype2=GamePanel::greedybyfront;
            }
            else if (selectgamepanel->selectgamedialog->tbtngreedyback2->GetValue())
            {
                greedytype2=GamePanel::greedybyfront;
            }

            if (selectgamepanel->selectgamedialog->tbtnslow->GetValue())
            {
                gamespeed=GamePanel::slow;
            }
            else if (selectgamepanel->selectgamedialog->tbtnmedium->GetValue())
            {
                gamespeed=GamePanel::medium;
            }
            else if (selectgamepanel->selectgamedialog->tbtnfast->GetValue())
            {
                gamespeed=GamePanel::fast;
            }
            wxPuts(wxT("d5\n"));
            //construct gamepanel aivsai;
        }

        wxPuts(wxT("d6\n"));

        //construct gamepanel;
        wxPuts(wxT("selesai passing\n"));
        this->RemoveChild(selectgamepanel);
        delete(selectgamepanel);
        wxPuts(wxT("selesai delete\n"));
        Refresh();
        this->Iconize(true);
        gamepanel = new GamePanel(this,wxID_ANY,isnewgame,sourcefile,boardsize,gametype,
              playername,greedytype,greedytype1,greedytype2,gamespeed);

    }

}
